package com.kong.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kong.constant.MessageConstant;
import com.kong.dao.CheckItemDao;
import com.kong.entity.PageResult;
import com.kong.pojo.CheckItem;
import com.kong.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 检查项服务实现类
 */
@Service
//@Transactional 2.6.2
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }

    @Override
    public PageResult findByPage(Integer pageNum, Integer pageSize,String queryString) {
        PageHelper.startPage(pageNum,pageSize);
        List<CheckItem> byPage = checkItemDao.findByPage(queryString);
        PageInfo pageInfo=new PageInfo(byPage);
        PageResult pageResult=new PageResult(pageInfo.getTotal(),byPage);
        return pageResult;
    }

    @Override
    public void addCheckItem(CheckItem checkItem) {
        int effectNum=checkItemDao.addOne(checkItem);
        if (effectNum<=0){
            throw  new RuntimeException();
        }
    }

    @Override
    public void deleteById(Integer id) {
        int count=checkItemDao.findById(id);
        if (count>0){
            throw new RuntimeException(MessageConstant.DELETE_CHECKITEM_FAIL2);
        }
        checkItemDao.deleteById(id);
    }

    @Override
    public void updateCheckItem(CheckItem checkItem) {
        /*
        判断该检查项是否存在--都需要操作数据库所以无需检测，在更新条件上加上id判断
         */
        int effectNum=checkItemDao.updateCheckItem(checkItem);
        if (effectNum<=0){
            throw new RuntimeException();
        }
    }

    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem=checkItemDao.findCheckItemById(id);
        return checkItem;
    }
}
